/**
This file is part of Gel2DE.

    Gel2DE is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Gel2DE is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Gel2DE.  If not, see <http://www.gnu.org/licenses/>.

	Copyright (C) 2012, University of Bergen
*/

#include "TransferFunctionManager.h"
#include "tinyXML.h"
#include "File.h"




TransferFunctionManager::TransferFunctionManager(vtkFileOutputWindow *aLogFile)
{
	m_logFile = aLogFile;
  if (m_ctfList.empty()){
    vtkColorTransferFunction * ctf = vtkColorTransferFunction::New();
    ctf->AddRGBPoint(-1, 200./255., 100./255., 0);
    ctf->AddRGBPoint(0, 1, 1, 1);
    ctf->AddRGBPoint(1, 0, 1, 0);

    // adding a default cft
    m_ctfList.push_back(ctf);
	m_currentCtf = 0;
  }
}

//For now, holds only one ctf since app does not allow for changing it.
void TransferFunctionManager::LoadCTF(std::string aFileName)
{
	m_ctfList.clear();
	TiXmlDocument doc;
	if(!doc.LoadFile(aFileName.c_str()))
	{
		std::string errMsg("ERROR: Could not open color transfer function " + aFileName);
		m_logFile->DisplayText(errMsg.c_str());
	}
	else
	{
		std::string infoMsg("Reading color transfer function " + aFileName + "...");
	}

	TiXmlNode* node;

	for(node = doc.FirstChild(); node != 0; node = node->NextSibling()){
		std::string tagName = node->Value();
		if(node->Type() == TiXmlNode::TINYXML_ELEMENT && tagName == "CTF") {
			vtkColorTransferFunction * ctf = vtkColorTransferFunction::New();
			TiXmlNode* giNode;
			for(giNode = node->FirstChild(); giNode != 0; giNode = giNode->NextSibling())
			{
				if(giNode->Type() == TiXmlNode::TINYXML_ELEMENT) {
					TiXmlElement *el = (TiXmlElement *)giNode;
					std::string elmName = el->Value();

					if (elmName == "CTFName"){
						std::string ctfName = el->GetText();
					}
					else if (elmName == "RGBPoint") {
						double x=0,r=0,b=0,g=0;
						el->QueryDoubleAttribute("x", &x);
						el->QueryDoubleAttribute("r", &r);
						el->QueryDoubleAttribute("g", &g);
						el->QueryDoubleAttribute("b", &b);
						ctf->AddRGBPoint(x, r, g, b);
					}
				}
			}
			m_ctfList.push_back(ctf);
		}
	}
}

TransferFunctionManager::~TransferFunctionManager(void)
{
  for (unsigned int i=0;i<m_ctfList.size();i++)
    m_ctfList[i]->Delete();
}

unsigned int TransferFunctionManager::setCurrentCtf(unsigned int ctfId) 
{
  if (ctfId >= m_ctfList.size())
    ctfId = m_ctfList.size() - 1;
  m_currentCtf = ctfId;
  return m_currentCtf;
}

vtkColorTransferFunction * TransferFunctionManager::getTransferFunction(unsigned int nr)
{
  if (nr < m_ctfList.size())
    return m_ctfList[nr];
  else
    return NULL;
}

vtkColorTransferFunction * TransferFunctionManager::getCurrentTransferFunction()
{
    return m_ctfList[m_currentCtf];
}

unsigned int TransferFunctionManager::getSize()
{
  return m_ctfList.size();
}

void TransferFunctionManager::getColor(double v, double &red, double &green, double &blue)
{
  red   = m_ctfList[m_currentCtf]->GetRedValue(v);
  green = m_ctfList[m_currentCtf]->GetGreenValue(v);
  blue  = m_ctfList[m_currentCtf]->GetBlueValue(v);
}

double TransferFunctionManager::getRed(double v) {
  return m_ctfList[m_currentCtf]->GetRedValue(v);
}

double TransferFunctionManager::getGreen(double v) {
  return m_ctfList[m_currentCtf]->GetGreenValue(v);
}

double TransferFunctionManager::getBlue(double v) {
  return m_ctfList[m_currentCtf]->GetBlueValue(v);
}